#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Farmer Li, 公众号: 很酷的程序员/RealCoolEngineer
# @Date: 2021-07-14
# @Question: https://www.zhihu.com/question/472192132

import re
import time


def end_zeros2(n):
    pat_ = re.compile(r'(?:[0]*$)')
    res = pat_.findall(str(n))
    return len(res[0])


def end_zeros1(num: int) -> int:
    num_str = str(num)
    num_str = num_str[::-1]
    count = 0

    for i in num_str:
        if i == '0':
            count += 1
        else:
            break
    return count


def zero_num_at_end(x: int):
    if x == 0:
        return 1

    num = 0
    while x >= 10:
        if x % 10 == 0:
            num += 1
            x = x // 10
        else:
            break
    return num


def zero_num_at_end_str(x: int):
    s1 = str(x)
    s2 = s1.rstrip('0')
    return len(s1) - len(s2)


def performance_test(func, times=100000):
    start = time.time()

    for i in range(times):
        n = func(i)

    end = time.time()
    print(f'"{func.__name__}" \tcall {times} times cost {end - start} seconds')


def main():
    testset = [0, 1, 10, 101, 245, 100100]
    for x in testset:
        n = zero_num_at_end(x)
        print(f'{x}\t end with {n} zeros')

    # Performance
    performance_test(zero_num_at_end)
    performance_test(zero_num_at_end_str)
    performance_test(end_zeros1)
    performance_test(end_zeros2)


if __name__ == '__main__':
    main()
